//
//  SinaOauthViewController.m
//  oc微博
//
//  Created by 安陆与 on 16/1/14.
//  Copyright © 2016年 安陆与. All rights reserved.
//

//let app_Key = "4094036286"
//let app_Secret = "ba6e1d99887953f43ef9e698755c574f"
//let redirect_Url = "http://www.baidu.com"


#import "SinaOauthViewController.h"
#import "AFNetworking.h"
#import "SVProgressHUD.h"
#import "AFNetworkActivityIndicatorManager.h"

#import "SinaAccountModel.h"
#import "SinaAccountViewModel.h"

#define app_Key @"4094036286"
#define app_Secret @"ba6e1d99887953f43ef9e698755c574f"
#define redirect_Url @"http://www.baidu.com"

@interface SinaOauthViewController ()<UIWebViewDelegate>

@property (nonatomic,weak) UIWebView *view;



@end

@implementation SinaOauthViewController

-(void)loadView{

    self.view = [[UIWebView alloc]init];

    [self setupNav];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadWebView];
    
}

/**
 *  这里写webView的代理方法
 *
 */
-(void)loadWebView{

    self.view.backgroundColor = [UIColor whiteColor];
    //创建一个URL
    NSURL *url = [NSURL URLWithString:@"https://api.weibo.com/oauth2/authorize?client_id=4094036286&redirect_uri=http://www.baidu.com"];
    
    //请求
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    //加载网页
    [self.view loadRequest:request];
    
    //设置代理
    self.view.delegate = self;

}

/**
 *  这里是转菊花的界面
 *
 */
-(void)webViewDidStartLoad:(UIWebView *)webView{

    [SVProgressHUD show];

}

-(void)webViewDidFinishLoad:(UIWebView *)webView{

    [SVProgressHUD dismiss];
}

-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{

    [SVProgressHUD dismiss];
}

/**
 *  最重要的一个代理方法
 *
 */
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    
    NSString *code;
    
//    NSString *redirect_Url = @"http://www.baidu.com";
    NSString *str = request.URL.absoluteString;
    
    if (str != nil) {
        if ([str hasPrefix:redirect_Url]) {
            if ((code = request.URL.query)) {
                NSString *subStr = [code substringFromIndex:5];
                SinaAccountViewModel *model = [[SinaAccountViewModel alloc]init];
//                SinaAccountViewModel *model = [SinaAccountViewModel shareAccount];
                [model getAccessTokenWithCode:subStr];
                
                //当授权认证成功后,发布第一个通知,来实现页面跳转
                [[NSNotificationCenter defaultCenter]postNotificationName:@"change" object:self];
            }
        }
    }

    return YES;
}



//设置导航控制器
-(void)setupNav{

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:(UIBarButtonItemStylePlain) target:self action:@selector(dismiss)];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"自动填充" style:(UIBarButtonItemStylePlain) target:self action:@selector(autofill)];

}

-(void)dismiss{

    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
    
}

-(void)autofill{

    NSString *str = @"document.getElementById('userId').value = \"13803439243\";document.getElementById('passwd').value = \"anluyu0315\"";
    
    [self.view stringByEvaluatingJavaScriptFromString:str];
}

@end



















